package com.leyou.user.controller;

import com.leyou.common.exception.pojo.LyException;
import com.leyou.user.entity.*;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 验证用户名或者手机号是否唯一
     */
    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkUsernameOrPhone(@PathVariable("data") String data,
                                                        @PathVariable("type") Integer type){
        Boolean isCanUse = userService.checkUsernameOrPhone(data, type);
        return ResponseEntity.ok(isCanUse);
    }

    /**
     * 发送短信验证码
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone){
        userService.sendCode(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 注册
     * BindingResult 获取hibernate校验数据的结果，一定要紧跟着需要被校验的对象后面
     */
    @PostMapping("/register")
    public ResponseEntity<Void> register(@Valid User user,
                                         BindingResult result,
                                         @RequestParam("code") String code){
        //收集到hibernate的校验异常，并封装成自定义异常
        if(result.hasErrors()){
            String errors = result.getFieldErrors()
                    .stream()
                    .map(FieldError::getDefaultMessage)
                    .collect(Collectors.joining("|"));
            throw new LyException(501, errors);
        }
        userService.register(user, code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 校验用户名和密码是否正确
     */
    @GetMapping("/query")
    public ResponseEntity<User> findUsernameAndPassword(@RequestParam("username") String username,
                                                        @RequestParam("password") String password){
        User user = userService.findUsernameAndPassword(username, password);
        return ResponseEntity.ok(user);
    }



    /**
     * 查所有省份
     */
    @GetMapping("/findAllProvince")
    public ResponseEntity<List<Province>> findAllProvince(){
        List<Province> provinceList = userService.findAllProvicne();
        return ResponseEntity.ok(provinceList);
    }

    /**
     * 根据省份id查询该省的所有的市
     */
    @PostMapping("/findCitiesByProvinceId")
    public ResponseEntity<List<City>> findCitiesByProvinceId(@RequestBody String provinceId){
        provinceId = provinceId.substring(0,6);
        List<City> cities = userService.findCitiesByProvinceId(provinceId);
        return ResponseEntity.ok(cities);
    }

    /**
     * 根据市id查询该市的所有地区
     */
    @PostMapping("/findAreaByCityId")
    public ResponseEntity<List<Area>> findAreaByCityId(@RequestBody String cityId){
        cityId = cityId.substring(0,6);
        List<Area> area = userService.findAreaByCityId(cityId);
        return ResponseEntity.ok(area);
    }

    /**
     * 保存用户信息
     */
    @PostMapping("/saveUserMessage")
    public ResponseEntity<Void> saveUserMessage(@RequestBody UserMessage userMessage,
                                                HttpServletRequest request){
        userService.saveUserMessage(userMessage,request);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询用户信息
     */
    @PostMapping("/findUserMessage")
    public ResponseEntity<UserMessage> findUserMessage(HttpServletRequest request){
        UserMessage userMessage = userService.findUserMessage(request);
        return ResponseEntity.ok(userMessage);
    }

}
